import Toast from 'tdesign-miniprogram/toast/index';
import drawQrcode from '../../utils/qrcode.js';
Page({
  data: {
    confirmBtn: { content: '确定', variant: 'base' },
    dialogKey: '',
    showConfirm: false,
    visible: false,
    list: [],
    currentOrderId: '',
  },

  goodListPagination: {
    index: 0,
    num: 20,
  },

  privateData: {
    tabIndex: 0,
  },

  getList() {
    const personId = wx.getStorageSync('personId');

    wx.request({
      url: `http://8.130.99.108:8090/api/getAppointmentList`, //仅为示例，并非真实的接口地址
      data: {
        patientId: personId,
      },
      method: 'GET',
      success: (res) => {
        console.log(res.data.data);
        this.setData({
          list: res.data?.data,
        });
      },
      fail: (error) => {
        console.log(error);
      },
    });
  },

  onLoad() {
    // this.getList();
  },
  onShow() {
    console.log('777');
    this.getList();
    this.getTabBar().init();
  },

  onClose({ detail }) {
    console.log(detail.trigger);
  },
  showDialog(e) {
    const { key, item } = e.currentTarget.dataset;
    console.log(item);
    this.setData({ [key]: true, dialogKey: key });
  },
  closeDialog() {
    const { dialogKey } = this.data;
    this.setData({ [dialogKey]: false });
  },
  confirmPre() {
    const { dialogKey, currentOrderId } = this.data;
    this.setData({ [dialogKey]: false });
    wx.request({
      url: `http://8.130.99.108:8090/api/cancelOrder?orderId=${currentOrderId}`, //仅为示例，并非真实的接口地址
      data: {
        // orderId: currentOrderId + '',
      },
      header: {
        'content-type': 'application/json', // 默认值
      },
      method: 'DELETE',
      success: (res) => {
        if (res.data.message == '服务运行成功') {
          Toast({
            context: this,
            selector: '#t-toast',
            message: '取消预约成功',
          });
          this.getList();
          this.setData({
            visible: false,
          });
        } else {
          Toast({
            context: this,
            selector: '#t-toast',
            message: '取消预约失败',
          });
        }
      },
      fail: (error) => {
        console.log(error);
      },
    });
  },

  onVisibleChange(e) {
    this.setData({
      visible: e.detail.visible,
    });
  },

  handlePopup(e) {
    const orderId = e.currentTarget?.dataset?.orderid;
    this.setData({
      currentOrderId: orderId,
    });
    drawQrcode({
      width: 200, // 必须，二维码宽度，与canvas的width保持一致

      height: 200, // 必须，二维码高度，与canvas的height保持一致

      canvasId: 'myQrcode',

      background: '#ffffff', //	非必须，二维码背景颜色，默认值白色

      foreground: '#2bb15e', // 非必须，二维码前景色，默认值黑色 	'#000000'

      // ctx: wx.createCanvasContext('myQrcode'), // 非必须，绘图上下文，可通过 wx.createCanvasContext('canvasId') 获取，v1.0.0+版本支持

      text: orderId + '', // 必须，二维码内容
      // v1.0.0+版本支持在二维码上绘制图片

      image: {
        // imageResource: '../../images/icon.png', // 指定二维码小图标
        dx: 70,
        dy: 70,
        dWidth: 60,
        dHeight: 60,
      },
    });

    this.setData({
      visible: true,
    });
  },
});
